Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
The MouseDown event procedure has the same details. Let’s go over the arguments: • Button—Indicates which button was pressed: left button = 1, right button = 2, center button = 4. • Shift—Indicates the state of the keyboard when the event occurred, whether the Shift, Ctrl, and/or Alt keys were pressed: Shift = 1, Ctrl = 2, Alt = 4. If more than one of these keys is pressed, the values are summed. For example, if Ctrl+Shift is pressed, then Shift is equal to 3. • X and Y—Indicates the position where the mouse pointer was located in the object’s coordinate system when the event occurred. When testing for buttons and/or Shift keys, you can use the predefined constants shown in Table 12.4. As you can see, the MouseUp and MouseDown event procedures have everything we need. We can determine not only which button was clicked, but at which location. TIP: Mouse-Related Events Visual Basic has four events related to clicking the mouse. If you’re not clear about how they relate to each other, you may end up with some subtle and hard-to-find bugs in your programs. The MouseDown event occurs when the user presses a mouse button. The object that the mouse pointer is over then captures the mouse. This means that the object will receive all mouse events up to and including the MouseUp event when the mouse button is released—even if the mouse has been moved off the object while the button was down. The Click event, however, is generated only if both the MouseDown and MouseUp events occur on the object. If the mouse is moved off the object before releasing the button, the object receives a MouseDown and a MouseUp event, but no Click event. If the button is released on the object, then the events occur in the order MouseDown, MouseUp, Click. If you double-click on an object, the event order is MouseDown, MouseUp, DblClick, MouseUp. Now we can get to the code that does the actual work. If we detect a left-button click, we’ll want to perform the following steps: 1. Calculate the coordinates of a rectangle centered on the click location using the magnification constants defined in the declarations section of the program. 2. Check that the rectangle does not extend past the edges of the source picture. If it does, adjust it. Table 12.4 Constants used to test for buttons and shift keys.
Constant | Value | Description
--- | --- | ---
vbLeftButton | 1 | Left button is pressed
vbRightButton | 2 | Right button is pressed
vbMiddleButton | 4 | Middle button is pressed
vbShiftMask | 1 | Shift key is pressed
vbCtrlMask | 2 | Ctrl key is pressed
vbAltMask | 4 | Alt key is pressed
3. Call PaintPicture to copy the contents of this rectangle to the entire Picture2 Picture Box, thus magnifying it. If it’s a right-button click, we’ll follow these steps instead: 1. Use PaintPicture with the vbBlackness opcode to paint the entire destination Picture Box black. 2. Set up a nested loop. The outer loop will loop once for each of three rows, and the inner loop once for each of three columns. 3. In the inner loop, use PaintPicture with the vbWhiteness opcode to create nine white rectangles over the destination Picture Box, so that each one is slightly smaller than one-third of the Picture Box dimension. 4. Still in the inner loop, use PaintPicture with the vbSrcCopy opcode to put a small copy of the source picture on each of the white rectangles. The code for these actions is in the MouseUp event procedure, which is shown in Listing 12.5, along with the rest of the form’s code. Figure 12.7 shows the program after the user left-clicks on the source image, and Figure 12.8 shows the effects of a right-click. When you run the program, you may notice one thing: If you cover the program window with another window, then redisplay it, the picture in the right Picture Box does not reappear. This is because the control’s AutoRedraw property is at the default value of False. As a result, the graphic in the Picture Box is not persistent, as was discussed earlier in this chapter. If you set AutoRedraw to True, you’ll see the graphic is now persistent. Another way to achieve the same result without setting AutoRedraw to True is to put the statements that do the actual drawing (the calls to PaintPicture ) in the Picture Box’s Paint event procedure, where they will be executed every time the image needs to be refreshed. Figure 12.7 Using the PaintPicture method to magnify part of a picture. Figure 12.8 Using the PaintPicture method to create nine framed duplicates of a picture.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.